/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oimwrapper.testers.models;

import com.thortech.util.logging.Logger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.Test;
import org.oimwrapper.testers.models.RequestBean;
import org.oimwrapper.testers.models.RequestChildBean;
import org.oimwrapper.testers.models.RequestParentBean;

/**
 *
 */
public class JSONRequest {
    
    private static final Logger logger = Logger.getLogger(JSONRequest.class.getName());
    
    public static final String CREATE_RESOURCE_C = 
        "{" +
        "\"UD_GRES_O_REQUESTTYPE\":\"CREATEUSER\"," +
        "\"UD_GRES_O_RESOURCENAME\":\"Resource C\"," +
        "\"UD_GRES_O_REQUESTER\":\"FFORESTER\"," +
        "\"UD_GRES_O_TARGETUSERID\":\"AA10310\"," +
        "\"UD_GRES_O_EXTERNALREQID\":\"99099\"" +
        "\"PARENTDATA\":[{\"UD_GRES_OA_NAME\":\"Favorite Color\",\"UD_GRES_OA_VALUE\":\"Blue\"}]" +
        "\"CHILDDATA\":[{\"UD_GRES_OC_SEQNUM\":\"1\",\"UD_GRES_OC_NAME\":\"Role Name\",\"UD_GRES_OC_VALUE\":\"VENDOR\"},{\"UD_GRES_OC_SEQNUM\":\"2\",\"UD_GRES_OC_NAME\":\"Role Name\",\"UD_GRES_OC_VALUE\":\"VENDOR2\"}]" +
        "}";
    
    public static final String CREATE_RESOURCE_C_GROUP = 
        "{" +
        "\"UD_GRES_O_REQUESTTYPE\":\"ADDROLE\"," +
        "\"UD_GRES_O_RESOURCENAME\":\"Resource C\"," +
        "\"UD_GRES_O_REQUESTER\":\"FFORESTER\"," +
        "\"UD_GRES_O_TARGETUSERID\":\"AA10310\"," +
        "\"UD_GRES_O_EXTERNALREQID\":\"99099\"" +
        "\"CHILDDATA\":[{\"UD_GRES_OC_SEQNUM\":\"1\",\"UD_GRES_OC_NAME\":\"Role Name\",\"UD_GRES_OC_VALUE\":\"VENDOR3\"},{\"UD_GRES_OC_SEQNUM\":\"2\",\"UD_GRES_OC_NAME\":\"Role Name\",\"UD_GRES_OC_VALUE\":\"VENDOR4\"}]" +
        "}";
    
    public static final String REMOVE_RESOURCE_C_GROUP = 
        "{" +
        "\"UD_GRES_O_REQUESTTYPE\":\"REMOVEROLE\"," +
        "\"UD_GRES_O_RESOURCENAME\":\"Resource C\"," +
        "\"UD_GRES_O_REQUESTER\":\"FFORESTER\"," +
        "\"UD_GRES_O_TARGETUSERID\":\"AA10310\"," +
        "\"UD_GRES_O_EXTERNALREQID\":\"99099\"" +
        "\"CHILDDATA\":[{\"UD_GRES_OC_SEQNUM\":\"1\",\"UD_GRES_OC_NAME\":\"Role Name\",\"UD_GRES_OC_VALUE\":\"VENDOR3\"},{\"UD_GRES_OC_SEQNUM\":\"2\",\"UD_GRES_OC_NAME\":\"Role Name\",\"UD_GRES_OC_VALUE\":\"VENDOR4\"}]" +
        "}";
    
    @Test
    public void mainTest()
    {
        RequestBean rb = getRequestFromJSON(JSONRequest.CREATE_RESOURCE_C);
        logger.debug("" + rb);
    }
    
    public RequestBean getRequestFromJSON(String request)
    {
        logger.debug("getResourceCRequest:" + request);
        try
        {
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(request);
            JSONObject jsonObject = (JSONObject)obj;
            logger.debug("obj:" + jsonObject);
            
            RequestBean rb = new RequestBean();
            BeanUtils.populate(rb, jsonObject);
            
            JSONArray parentData = (JSONArray)jsonObject.get("PARENTDATA");
            JSONArray childData = (JSONArray)jsonObject.get("CHILDDATA");
            
            Iterator<JSONObject> i = null;
            List parentList = new ArrayList();
            if (parentData != null)
            {
                i = parentData.iterator();
                while(i.hasNext())
                {
                    JSONObject rec = (JSONObject)i.next();
                    RequestParentBean rpb = new RequestParentBean();
                    BeanUtils.populate(rpb, rec);
                    parentList.add(rpb);
                    //logger.debug("" + rpb);
                }
            }
            
            List childList = new ArrayList();
            if (childData != null)
            {
                i = childData.iterator();
                while(i.hasNext())
                {
                    JSONObject rec = (JSONObject)i.next();
                    RequestChildBean rcb = new RequestChildBean();
                    BeanUtils.populate(rcb, rec);
                    childList.add(rcb);
                    //logger.debug("" + rcb);
                }
            }
            
            rb.setPARENTDATA(parentList);
            rb.setCHILDDATA(childList);
            
            return rb;
        }
        catch(Exception e)
        {
            logger.error("JSONE",e);
            return null;
        }
    }

}
